//
//  SHD_SearDataViewController.m
//  B段项目
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_SearDataViewController.h"
#import "GZ_PlayerPageVC.h"
@interface SHD_SearDataViewController ()<SHD_delegate>

@end

@implementation SHD_SearDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(0, 0, 75 * FitWidth, 75 * FitHeight)];
    [self.view addSubview:_allLabel];
    _userLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(75 * FitWidth, 0, 75 * FitWidth, 75 * FitHeight)];
    [self.view addSubview:_userLabel];
    _tingLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(75 * FitWidth * 2, 0, 75 * FitWidth, 75 * FitHeight)];
    [self.view addSubview:_tingLabel];
    _contentLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(75 * FitWidth * 3, 0, 75 * FitWidth, 75 * FitHeight)];
    [self.view addSubview:_contentLabel];
    _topicLabel = [[myTitleLabel alloc]initWithFrame:CGRectMake(75 * FitWidth * 4, 0, 75 * FitWidth, 75 * FitHeight)];
    [self.view addSubview:_topicLabel];
    _allLabel.userInteractionEnabled = YES;
     _contentLabel.userInteractionEnabled = YES;
     _userLabel.userInteractionEnabled = YES;
     _topicLabel.userInteractionEnabled = YES;
     _tingLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1)];
    [_allLabel addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2)];
    [_userLabel addGestureRecognizer:tap2];

    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap3)];
    [_tingLabel addGestureRecognizer:tap3];

    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap4)];
    [_contentLabel addGestureRecognizer:tap4];

    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap5)];
    [_topicLabel addGestureRecognizer:tap5];

    _allLabel.textAlignment = 1;
    _topicLabel.textAlignment = 1;
    _tingLabel.textAlignment = 1;
    _contentLabel.textAlignment = 1;
    _userLabel.textAlignment = 1;
    _allLabel.numberOfLines = 0;
    _userLabel.numberOfLines = 0;
    _tingLabel.numberOfLines = 0;
    _topicLabel.numberOfLines = 0;
    _contentLabel.numberOfLines = 0;
    _allLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 75 * FitHeight, 375 *FitWidth, 1 *FitHeight)];
    [self.view addSubview:label];
    label.backgroundColor = [UIColor grayColor];
    label.alpha = 0.5;
    _downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight)];
    _downLabel.backgroundColor = [UIColor colorWithRed:0.55 green:0.78 blue:0.63 alpha:1];
    [self.view addSubview:_downLabel];
    _userLabel.textColor = [UIColor lightGrayColor];
    _tingLabel.textColor = [UIColor lightGrayColor];
    _contentLabel.textColor = [UIColor lightGrayColor];
    _topicLabel.textColor = [UIColor lightGrayColor];
    _myCollectionView.delegate = self;
    [_myCollectionView NightWithType:UIViewColorType2];
    
}
#pragma mark 按钮方法
-(void)tap1
{
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(0, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    _allLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    _userLabel.textColor = [UIColor lightGrayColor];
    _tingLabel.textColor = [UIColor lightGrayColor];
    _contentLabel.textColor = [UIColor lightGrayColor];
    _topicLabel.textColor = [UIColor lightGrayColor];
    [_myCollectionView setContentOffset:CGPointMake(375 * 0 * FitWidth, 0) animated:YES];

}
-(void)tap2
{
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(75 * FitWidth , 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    _userLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    _allLabel.textColor = [UIColor lightGrayColor];
    
    _tingLabel.textColor = [UIColor lightGrayColor];
    _contentLabel.textColor = [UIColor lightGrayColor];
    _topicLabel.textColor = [UIColor lightGrayColor];
    [_myCollectionView setContentOffset:CGPointMake(375 * 1* FitWidth, 0) animated:YES];

}

-(void)tap3
{
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(75 * FitWidth * 2, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    _tingLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    _allLabel.textColor = [UIColor lightGrayColor];
    _userLabel.textColor = [UIColor lightGrayColor];
        _contentLabel.textColor = [UIColor lightGrayColor];
    _topicLabel.textColor = [UIColor lightGrayColor];
    [_myCollectionView setContentOffset:CGPointMake(375 * 2 * FitWidth, 0) animated:YES];
    

}

-(void)tap4
{
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(75 * FitWidth * 3, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    _contentLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    _allLabel.textColor = [UIColor lightGrayColor];
    _userLabel.textColor = [UIColor lightGrayColor];
    _tingLabel.textColor = [UIColor lightGrayColor];
        _topicLabel.textColor = [UIColor lightGrayColor];
    [_myCollectionView setContentOffset:CGPointMake(375 * 3* FitWidth, 0) animated:YES];

}

-(void)tap5
{
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(75 * FitWidth *4, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    _topicLabel.textColor = [UIColor colorWithRed:0.16 green:0.98 blue:0.58 alpha:1];
    _allLabel.textColor = [UIColor lightGrayColor];
    _userLabel.textColor = [UIColor lightGrayColor];
    _tingLabel.textColor = [UIColor lightGrayColor];
    _contentLabel.textColor = [UIColor lightGrayColor];
    [_myCollectionView setContentOffset:CGPointMake(375 * 4 * FitWidth, 0) animated:YES];
   

}
#pragma mark 执行协议方法
-(void)delegateMethond:(NSInteger)index
{
    [_myCollectionView setContentOffset:CGPointMake(375 * index * FitWidth, 0) animated:YES];
        switch (index) {
            case 0:
                [self tap1];
                break;
            case 1:
                [self tap2];
                break;
            case 2:
                [self tap3];
                break;
            case 3:
                [self tap4];
                break;
            case 4:
                [self tap5];
                break;
    
            default:
                break;
        }

    
    
}
-(void)jumpVC:(NSString *)index
{
    GZ_PlayerPageVC *gz = [[GZ_PlayerPageVC alloc]init];
    [self.navigationController pushViewController:gz animated:YES];
    gz.tingString = index;

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger temp = scrollView.contentOffset.x / (375 * FitWidth);
    
    [UIView animateWithDuration:0.3 animations:^{
        _downLabel.frame = CGRectMake(75 * FitWidth * temp, 75 * FitHeight, 75 * FitWidth, 2 *FitHeight);
    }];
    
    switch (temp) {
        case 0:
            [self tap1];
            break;
            case 1:
            [self tap2];
            break;
            case 2:
            [self tap3];
            break;
            case 3:
            [self tap4];
            break;
            case 4:
            [self tap5];
            break;
            
        default:
            break;
    }
    
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _userArray = [NSMutableArray array];
    _tingArray = [NSMutableArray array];
    _topicArray = [NSMutableArray array];
    _contectArray = [NSMutableArray array];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(375 * FitWidth, 528 * FitHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = 1;
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 75 * FitHeight, 375 * FitWidth, 528 * FitHeight) collectionViewLayout:flowLayout];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.pagingEnabled = YES;
    [self.view addSubview:_myCollectionView];
    [_myCollectionView registerClass:[SHD_SearchDataCollectionCell class] forCellWithReuseIdentifier:@"identifier"];
    [_myCollectionView registerClass:[SHD_SearUserConllectCell class] forCellWithReuseIdentifier:@"useriden"];
    [_myCollectionView registerClass:[SHD_SearTingConllectCell class] forCellWithReuseIdentifier:@"tingiden"];
    [_myCollectionView registerClass:[SHD_SearTopicConllectCell class] forCellWithReuseIdentifier:@"topiciden"];
    [_myCollectionView registerClass:[SHD_SearContectConllectCell class] forCellWithReuseIdentifier:@"contectiden"];
    
    [self getData];
}
#pragma mark collectionCell 的两个方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHD_SearchDataCollectionCell *allCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    allCell.ID = _idString;
    allCell.delegate = self;
    allCell.jumpDelegate = self;
    allCell.nav = self.navigationController;
    SHD_SearUserConllectCell *userCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"useriden" forIndexPath:indexPath];
    userCell.ID = _idString;
    userCell.nav = self.navigationController;
    SHD_SearTingConllectCell *tingCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tingiden" forIndexPath:indexPath];
    tingCell.ID = _idString;
    tingCell.nav = self.navigationController;
    SHD_SearContectConllectCell *contectCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"contectiden" forIndexPath:indexPath];
    contectCell.nav = self.navigationController;
    contectCell.ID = _idString;
    SHD_SearContectConllectCell *topicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topiciden" forIndexPath:indexPath];
    topicCell.nav = self.navigationController;
    topicCell.ID = _idString;
    

    

    switch (indexPath.row) {
        case 0:
            return allCell;
            case 1:
            return userCell;
            break;
            case 2:
    
            return tingCell;
            break;
            case 3:
            return contectCell;
            break;
            case 4:
            return topicCell;
            break;
            
            
       
    }
    return contectCell;
    
}
-(void)getData
{
   
    
    [HTTPMethod getDataByString:@"http://api2.pianke.me/search/index" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&keyword=%@&version=3.0.6", _idString]] WithDataBlock:^(id data) {
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"alldetail"];
        _allLabel.text = [NSString stringWithFormat:@"全部\n%@", [[data objectForKey:@"data"] objectForKey:@"alltotal"]];
        _userLabel.text = [NSString stringWithFormat:@"用户\n%@",  [array[0] objectForKey:@"total"]];
         _tingLabel.text = [NSString stringWithFormat:@"Ting\n%@",  [array[1] objectForKey:@"total"]];
         _contentLabel.text = [NSString stringWithFormat:@"故事\n%@",  [array[2] objectForKey:@"total"]];
         _topicLabel.text = [NSString stringWithFormat:@"话题\n%@",  [array[3] objectForKey:@"total"]];
      
      
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
